package com.ufgov.agcfs.baseData.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import uap.iweb.entity.DataTable;
import uap.iweb.exception.WebRuntimeException;
import uap.iweb.icontext.IWebViewContext;

import com.ufgov.agcfs.baseData.entity.BookType;
import com.ufgov.agcfs.baseData.entity.UpdateType;
import com.ufgov.agcfs.baseData.service.UpdateTypeService;
import com.ufgov.agcfs.report.entity.Report;
import com.ufgov.ip.base.service.IPDataTableService;



@Controller("baseData.updateType.updateTypeController")
@Scope("prototype")
@RequestMapping(value = "/baseData/updateType")
public class UpdateTypeController {
private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UpdateTypeService updateService;
	@Autowired 
	IPDataTableService iPDataTableService;
	
	DataTable<UpdateType> updateDataTable;
	
	public void loadUpdateType() {
		try {
			Map<String, Object> searchParams = new HashMap<String, Object>();
			searchParams = iPDataTableService.createSearchParamsStartingWith("search_EQ_",updateDataTable);
			String nd = (String) searchParams.get("nd");
			List<UpdateType> updateList=updateService.findByNd(nd);
			updateDataTable.remove(updateDataTable.getAllRow());
			updateDataTable.set(updateList.toArray(new UpdateType[0]));	
			IWebViewContext.getResponse().write("true");							
		} catch (Exception e) {
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}
	}
	
	
	@RequestMapping(method = RequestMethod.GET,value="getUpdateList")
	public  @ResponseBody HashMap<String,Object> getUpdateList(HttpServletRequest request){
		try {
			String nd = request.getParameter("extra_search");
			if (nd==null) nd ="";
			List<UpdateType> updtList = updateService.getUpdateList(nd);
//			int updtCnt = updtList.size();
			HashMap<String,Object> hashRet = new HashMap<String, Object>();
//			hashRet.put("draw", draw);
//			hashRet.put("recordsTotal",updtCnt);
//			hashRet.put("recordsFiltered",updtCnt);
			hashRet.put("data",updtList);
			return hashRet;
		} catch (Exception e) {
			logger.error(e.getMessage());
			IWebViewContext.getResponse().write("false");
			throw new WebRuntimeException("查询数据失败!");
		}		
	}
	
	@RequestMapping(method = RequestMethod.GET,value="getAllNd")
	public @ResponseBody List<String> getAllnd(HttpServletRequest request, HttpServletResponse response){
		List<String> ndList = new ArrayList<String>();
		try {
			ndList = updateService.getAllNd();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ndList;		
	}
	
	
	@RequestMapping(method = RequestMethod.POST,value="saveUpdateType")
	public  @ResponseBody Map<String, String> saveUpdateType(@RequestBody UpdateType updateType,HttpServletRequest request, HttpServletResponse response){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "保存成功！");
		try {
			updateService.saveUpdateTypeService(updateType,resultMap);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			resultMap.put("result", "fail");
			resultMap.put("reason", "服务端繁忙，请稍后重试！");
		}
		return resultMap;
	}
	
	
	@RequestMapping(method = RequestMethod.POST,value="delUpdateType")
	public @ResponseBody Map<String, String> delUpdateType(HttpServletRequest request){
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("result", "success");
		resultMap.put("reason", "删除成功！");
		try {
			String[] ids = request.getParameter("ids").split(",");
			List<String> uptIds = new ArrayList<String>();
			Collections.addAll(uptIds, ids);
		//	String id = request.getParameter("id");
		//	String nd = request.getParameter("nd");
		//	String updateTypeCode = request.getParameter("updateType");
			updateService.delUpdateType(uptIds,resultMap);
		} catch (Exception e) {
			// TODO 自动生成的 catch块
			resultMap.put("result", "fail");
			resultMap.put("reason", "服务端繁忙，请稍后重试！");
		}
		return resultMap;
		
	}	
}
